package cheqex.update;

import javax.swing.*;

import cheqex.dao.ReportPeer;
import cheqex.update.controllers.ActivitiesController;
import cheqex.update.controllers.CashInBankController;
import cheqex.update.controllers.DateController;

import java.awt.*;
import java.awt.event.*;

public class CheqexFrame extends JFrame implements ActionListener{
	private JLabel dateField;
	
	public JButton dailyCollection;
	public JButton cashInBank;
	public JButton accountSettings;
	
	private JPanel mainPanel;
	public JPanel rightPanel;
	private JPanel leftPanel;
	private JPanel summary;
	
	
	public CheqexFrame(){
		this.initialize();
	}
	
	//removes panel to the rightPanel
	private void removePanel(){
		this.rightPanel.removeAll();
	}
	
	//initializes components
	private void initialize(){
		String date = "Date: " + DateController.getDate().toString();
		JLabel dateField = new JLabel(date);
		
		//buttons
		dailyCollection = new JButton("Daily Collection Report");
		dailyCollection.setMnemonic(KeyEvent.VK_D);
	    dailyCollection.setActionCommand("updateDaily");
	    dailyCollection.setEnabled(true);
	    dailyCollection.addActionListener(this);
		
	    cashInBank = new JButton("Cash In Bank Report");
	    cashInBank.setMnemonic(KeyEvent.VK_D);
	    cashInBank.setActionCommand("updateWeekly");
	    cashInBank.setEnabled(true);
	    cashInBank.addActionListener(this);
	    
	    accountSettings = new JButton("Account Settings");
	    accountSettings.setMnemonic(KeyEvent.VK_D);
	    accountSettings.setActionCommand("updateSettings");
	    accountSettings.setEnabled(true);
	    accountSettings.addActionListener(this);
		
	    this.summary = new JPanel();
		
		//main panel (left & right) division
		leftPanel = new JPanel();
		leftPanel.setLayout(new GridLayout(5,1,0,5));
		leftPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder()," Welcome! "));
		leftPanel.add(dateField);
		leftPanel.add(dailyCollection);
		leftPanel.add(cashInBank);
		leftPanel.add(accountSettings);
		leftPanel.setVisible(true);
		
		rightPanel = new JPanel();
		rightPanel.setLayout(new BorderLayout());
		rightPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder()," "));
		rightPanel.setVisible(true);
		latestActivities();
		
		//main panel
		mainPanel = new JPanel();
		mainPanel.setLayout(new BorderLayout());
		mainPanel.add(leftPanel, BorderLayout.WEST);
		mainPanel.add(rightPanel, BorderLayout.CENTER);
		
		this.add(mainPanel);
		
	    this.setVisible(true);
		this.pack();
		this.setSize(650,400);
		this.setResizable(false);
		this.setTitle("CheQEx");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	private void latestActivities(){
		this.rightPanel.add(new ActivitiesView());
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		
		if ("updateDaily".equals(e.getActionCommand())) {
			removePanel();
			DateView date = new DateView(this);
			date.setVisible(true);
			
		}
		else
		if ("updateWeekly".equals(e.getActionCommand())) {
			removePanel();
			int weekId = CashInBankController.getCurrentWeekOfTheYear();
			int beginningBalance = CashInBankController.getBeginningBalance(weekId);
			int totalSumOfCollections = CashInBankController.getTotalSumOfCollections(weekId);
			int bankInterest = CashInBankController.getBankInterest(weekId);
			int totalSumOfWithdrawals = CashInBankController.getTotalSumOfWithdrawals(weekId);
			int totalCashInBankBalance = CashInBankController.getTotalCashInBankBalance(weekId);
			
			ReportPeer.insertCashInBankForWeek(weekId, beginningBalance, totalSumOfCollections, bankInterest, totalSumOfWithdrawals, totalCashInBankBalance);
			this.rightPanel.add(new CashInBankView());
			this.rightPanel.setVisible(true);
			this.pack();
		}
		else
		if ("updateSettings".equals(e.getActionCommand())) {
			removePanel();
			this.rightPanel.add(new AccountSettingsView());
			this.rightPanel.setVisible(true);
			this.pack();
				
		}
	}
}
